package com.example.teamtribe.common;

import android.os.Bundle;
import android.util.Log;

import com.example.teamtribe.json.JsonIds;
import com.example.teamtribe.utils.HttpUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class CallbackStore {

    public static abstract class CallbackImpl<T extends Back> implements okhttp3.Callback {

        public T back;

        public CallbackImpl(T back) {
            this.back = back;
        }

        @Override
        public void onFailure(Call call, IOException e) {
            back.failure(HttpUtil.getFailureMeg());
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String json = response.body().string();
            Log.d("网络", "来自CallbackImpl:::" + json);
            try {
                JSONObject object = new JSONObject(json);
                int status = object.getInt("code");
                if (status != 0) {
                    back.failure(HttpUtil.getCodeMeg(status));
                } else {
                    onResponse(json);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        public abstract void onResponse(String json);
    }

    public static Callback getCallback(final Back back) {
        return new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Log.d("getCallback", json);
                try {
                    JSONObject object = new JSONObject(json);
                    int status = object.getInt("code");
                    if (status != 0) {
                        back.failure(HttpUtil.getCodeMeg(status));
                    } else {
                        back.success(null);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
    }

    public static Callback getIdCallback(final Back back) {
        return new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("回调id", str);
                try {
                    JSONObject object = new JSONObject(str);
                    int status = object.getInt("code");
                    if (status != 0) {
                        back.failure(HttpUtil.getCodeMeg(status));
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putInt("id", object.getInt("id"));
                        back.success(bundle);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
    }

    public static Callback getIdsCallback(final Back back) {
        return new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("id数组", str);
                JsonIds jsonIds = new Gson().fromJson(str, JsonIds.class);
                int status = jsonIds.getCode();
                if (status != 0) {
                    back.failure(HttpUtil.getCodeMeg(status));
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putIntegerArrayList("ids", jsonIds.getIds());
                    back.success(bundle);
                }
            }
        };
    }
}
